import { reactive } from "vue";
import { Api } from "../../../api/api.js";
import { ElNotification } from 'element-plus';
// import router from "../../router/index.js";
import { GJ_nowTime } from "../../../../static/js/tool.js";
function userAddJS() {
    let status = reactive({
        sign: {
            user: "",
            passwd: "",
            email: "",
            tel: "",
            limit: 2,
            createTime: "",
            lastTime: ""
        },
        userinfo: JSON.parse(window.sessionStorage.getItem("userinfo")),
        islimit: true,
    })
    let methodFun = {
        //注册
        confirmSign: () => {
            let telReg = new RegExp("^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$");
            let emailReg = new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");
            let resultarr = [];
            resultarr.push(((status.sign.user) + "").length >= 6);
            resultarr.push(((status.sign.passwd) + "").length >= 6);
            resultarr.push(telReg.test(status.sign.tel));
            resultarr.push(emailReg.test(status.sign.email));
            console.log(resultarr, status.sign);
            let result = resultarr.every((item) => {
                return item;
            });
            if (!result) {
                ElNotification({
                    title: "注册失败",
                    type: "error",
                    message: "信息填写不完整或信息格式不正确",
                    duration: 1000,
                });
            } else {
                status.sign.createTime = GJ_nowTime("date-time", "-");
                status.sign.lastTime = GJ_nowTime("date-time", "-");
                Api.signAdd(status.sign).then((res) => {
                    if (res.data) {
                        ElNotification({
                            title: "注册成功",
                            type: "success",
                            message: "赶紧通知该用户使用新账号登录吧！",
                            duration: 1000,
                        });
                        methodFun.init();
                    } else {
                        ElNotification({
                            title: "注册失败",
                            type: "error",
                            message: "用戶名已存在",
                            duration: 1000,
                        });
                    }
                })
                    .catch((err) => {
                        console.log(err);
                    });
            }
        },
        //给用户设置权限
        changeLimit: () => {
            status.islimit = !status.islimit;
        },
        //初始化表单
        init: () => {
            status.sign.user = "";
            status.sign.passwd = "";
            status.sign.email = "";
            status.sign.tel = "";
            status.sign.limit = 2;
            status.sign.createTime = "";
            status.sign.lastTime = "";
        }
    }
    return {
        status,
        methodFun
    }
}

export default userAddJS;